package com.cjd.common.adapter

import android.widget.BaseAdapter
import com.cjd.common.holder.BaseGridAndListHolder
import java.util.*

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/7/3
 * description
 */
abstract class BaseGridAndListArrayAdapter<T, in VH : BaseGridAndListHolder> : BaseAdapter() {

    protected var data = ArrayList<T>()

    fun add(item: T) {
        data.add(item)
        notifyDataSetChanged()
    }

    fun add(index: Int, item: T) {
        data.add(index, item)
        notifyDataSetChanged()
    }

    fun addAll(collection: Collection<T>?) {
        if (collection != null) {
            data.addAll(collection)
            notifyDataSetChanged()
        }
    }

    fun replaceAll(collection: Collection<T>) {
        data.clear()
        addAll(collection)
    }

    fun getAll(): ArrayList<T> {
        return data
    }

    override fun getCount(): Int {
        return data.size
    }

    override fun getItem(position: Int): T {
        return data[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
}